/* From: http://www.abc.se/~m6695/udp.html */

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>

#define BUFLEN 512
#define NPACK 10
#define PORT 2003
#define SRV_IP "10.0.3.141"
// #define SRV_IP "127.0.0.1"

void diep(char *s)
{
  perror(s);
  exit(1);
}


int main(void)
{
  struct sockaddr_in si_other;
  int s, i, slen=sizeof(si_other);
  char buf[BUFLEN];

  if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    diep("socket");

  memset((char *) &si_other, 0, sizeof(si_other));
  si_other.sin_family = AF_INET;
  si_other.sin_port = htons(PORT);
  if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
    fprintf(stderr, "inet_aton() failed\n");
    exit(1);
  }

  time_t nNow =  time(NULL);
    sprintf(buf, "laptop.main.testValue 398 %lu\n", nNow);
    printf("Sending packet to %s@%d: %s", SRV_IP, PORT, buf);
    if (sendto(s, buf, strlen(buf), 0, (struct sockaddr*)&si_other, slen)==-1)
      diep("sendto()");

  close(s);
  return 0;
}
